package com.ddlc.anno.util;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;

import com.ddlc.anno.BindView;
import com.ddlc.anno.ClickView;
import com.ddlc.anno.ContentView;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * https://gitee.com/tangbuzhi
 *
 * @author: Tangbuzhi
 * @version:
 * @package:
 * @description:
 * @modify:
 * @date: 2018/4/3
 */

public class TangBind {
    //activity
    public static void bind(Object obj) {
        contentView(obj);
        bindView(obj);
        clickView(obj);
    }

    private static void contentView(Object obj) {
        Class<?> clz = obj.getClass();
        ContentView contentView = clz.getAnnotation(ContentView.class);
        if (contentView != null) {
            int contentLayoutResId = contentView.value();
            try {
                Method setContentView = clz.getMethod("setContentView", new Class[]{int.class});
                setContentView.invoke(obj, contentLayoutResId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void bindView(Object obj) {
        Class<?> clz = obj.getClass();
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {
            BindView bindView = field.getAnnotation(BindView.class);
            if (bindView != null) {
                int viewId = bindView.value();
                if (obj instanceof Activity) {
                    Activity activity = (Activity) obj;
                    View view = activity.findViewById(viewId);
                    field.setAccessible(true);
                    try {
                        field.set(obj, view);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void clickView(final Object obj) {
        Class<?> clz = obj.getClass();
        Method[] declaredMethods = clz.getDeclaredMethods();
        for (final Method declaredMethod : declaredMethods) {
            ClickView clickView = declaredMethod.getAnnotation(ClickView.class);
            if (clickView != null) {
                declaredMethod.setAccessible(true);
                Object clickListener = Proxy.newProxyInstance(View.OnClickListener.class.getClassLoader(),
                        new Class[]{View.OnClickListener.class}, new InvocationHandler() {
                            @Override
                            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                                return declaredMethod.invoke(obj, args);
                            }
                        });

                int[] viewIds = clickView.value();
                for (int viewId : viewIds) {
                    if (obj instanceof Activity) {
                        Activity activity = (Activity) obj;
                        View view = activity.findViewById(viewId);
                        try {
                            Method setOnClickListener = view.getClass().getMethod("setOnClickListener", new Class[]{View.OnClickListener.class});
                            setOnClickListener.invoke(view, clickListener);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (obj instanceof Fragment) {

                    }
                }
            }
        }
    }
}
